package com.baoleme.baoleme_goods_service.controller;

import com.baoleme.baoleme_goods_service.service.SetMealService;
import com.baoleme.common.Result;
import com.baoleme_goods_model.dto.SetMealDTO;
import com.baoleme_goods_model.entity.SetMeal;
import com.baoleme_goods_model.vo.SetMealForClientVO;
import com.baoleme_goods_model.vo.SetMealGoodsForClientVO;
import com.baoleme_goods_model.vo.SetMealVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2024-09-13
 */
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {

    @Resource
    private SetMealService setMealService;

    /**
     * 修改套餐
     * @param setMealDTO
     * @return 是否修改成功
     */
    @Operation(summary = "修改套餐")
    @PutMapping
    public Result<Boolean> updateSetMeal(@RequestBody SetMealDTO setMealDTO){
        return Result.success(setMealService.updateSetMeal(setMealDTO));
    }

    /**
     * 分页查询套餐
     * @param categoryId
     * @param name
     * @param currentStr
     * @param pageSizeStr
     * @param status
     * @return 分页内容
     */
    @Operation(summary = "分页查询套餐")
    @GetMapping("page")
    public Result<IPage<SetMeal>> page(@RequestParam(value = "categoryId",required = false) String categoryId, @RequestParam(value = "name",required = false) String name,@RequestParam String currentStr, @RequestParam String pageSizeStr, @RequestParam(value = "status",required = false) String status){
        LambdaQueryWrapper<SetMeal> wrapper = new LambdaQueryWrapper<>();
        if (categoryId != null && !categoryId.isEmpty()){
            wrapper.eq(SetMeal::getCategoryId,categoryId);
        }
        if (name != null && !name.isEmpty()){
            wrapper.like(SetMeal::getName,name);
        }
        if (status != null && !status.isEmpty()){
            wrapper.eq(SetMeal::getStatus,status);
        }
        wrapper.orderByDesc(SetMeal::getCreateTime);

        int current = Integer.parseInt(currentStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        IPage<SetMeal> pageInfo = new Page<>(current,pageSize);

        return Result.success(setMealService.page(pageInfo,wrapper));
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     * @return 是否修改成功
     */
    @Operation(summary = "套餐起售、停售")
    @PostMapping("status/{status}")
    public Result<Boolean> status(@PathVariable String status, @RequestParam String id){
        SetMeal setMeal = new SetMeal();
        setMeal.setId(id);
        setMeal.setStatus(Integer.parseInt(status));
        return Result.success(setMealService.updateById(setMeal));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return 是否批量删除成功
     */
    @Operation(summary = "批量删除")
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam List<String> ids){
        log.info("ids:{}",ids);
        return Result.success(setMealService.removeWithGoods(ids));
    }

    /**
     * 新增套餐
     * @param setMealDTO
     * @return 是否增加成功
     */
    @Operation(summary = "新增套餐")
    @PostMapping
    public Result<Boolean> add(@RequestBody SetMealDTO setMealDTO){
        return Result.success(setMealService.saveSetMeal(setMealDTO));
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return 套餐VO
     */
    @Operation(summary = "根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetMealVO> queryWithGoodsById(@PathVariable String id){
        return Result.success(setMealService.getWithGoodsById(id));
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId 分类id
     * @return
     */
    @Operation(summary = "根据分类id查询套餐")
    @GetMapping("list")
    public Result<List<SetMealForClientVO>> queryByCategoryId(String categoryId){
        return Result.success(setMealService.getByCategoryId(categoryId));
    }

    /**
     * 根据套餐id查询包含的商品
     * @param id 套餐id
     * @return 套餐包含的商品
     */
    @Operation(summary = "根据套餐id查询包含的商品")
    @GetMapping("goods/{id}")
    public Result<List<SetMealGoodsForClientVO>> querySetMealGoodsBySetMealId(@PathVariable String id){
        return Result.success(setMealService.getSetMealGoodsBySetMealId(id));
    }
}
